﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using lca.jrc.it.ILCD.Common;
using lca.jrc.it.ILCD.Categories;
using lca.jrc.it.ILCD.Contact;
using lca.jrc.it.ILCD.Flow;
using lca.jrc.it.ILCD.FlowProperty;
using lca.jrc.it.ILCD.LCIAMethod;
using lca.jrc.it.ILCD.Locations;
using lca.jrc.it.ILCD.Process;
using lca.jrc.it.ILCD.Source;
using lca.jrc.it.ILCD.UnitGroup;

namespace JRC.LCA.ILCD.SDK
{
    public class ELCD2Reader
    {
        const string version = "_02.00.000";

        string ilcdpath;
        public ELCD2Reader(string elcd2path)
        {
            ilcdpath = elcd2path + "\\ILCD";
        }

        public IEnumerable<contactDataSet> AllContacts
        {
            get
            {
                string[] files = Directory.GetFiles(ilcdpath + "\\contacts", "*.xml");
                foreach (var file in files)
                {
                    yield return contactDataSet.Load(file);
                }
            }
        }

        public contactDataSet GetContact(Guid uuid)
        {
            return contactDataSet.Load(ilcdpath + "\\contacts\\" + uuid.ToString() + version + ".xml");
        }

        public IEnumerable<flowDataSet> AllFlows
        {
            get
            {
                string[] files = Directory.GetFiles(ilcdpath + "\\flows", "*.xml");
                foreach (var file in files)
                {
                    yield return flowDataSet.Load(file);
                }
            }
        }

        public flowDataSet GetFlow(Guid uuid)
        { 
            return flowDataSet.Load(ilcdpath + "\\flows\\" + uuid.ToString() + version + ".xml");
        }

        public IEnumerable<flowPropertyDataSet> AllFlowProperties
        {
            get
            {
                string[] files = Directory.GetFiles(ilcdpath + "\\flowproperties", "*.xml");
                foreach (var file in files)
                {
                    yield return flowPropertyDataSet.Load(file);
                }
            }
        }

        public flowPropertyDataSet GetFlowProperty(Guid uuid)
        {
            return flowPropertyDataSet.Load(ilcdpath + "\\flowproperties\\" 
                + uuid.ToString() + version + ".xml");
        }

        public IEnumerable<processDataSet> AllProcesses
        {
            get
            {
                string[] files = Directory.GetFiles(ilcdpath + "\\processes", "*.xml");
                foreach (var file in files)
                {
                    yield return processDataSet.Load(file);
                }
            }
        }

        public processDataSet GetProcess(Guid uuid)
        {
            return processDataSet.Load(ilcdpath + "\\processes\\" + uuid.ToString() + version + ".xml");
        }

        public IEnumerable<sourceDataSet> AllSources
        {
            get
            {
                string[] files = Directory.GetFiles(ilcdpath + "\\sources", "*.xml");
                foreach (var file in files)
                {
                    yield return sourceDataSet.Load(file);
                }
            }
        }

        public sourceDataSet GetSource(Guid uuid)
        {
            return sourceDataSet.Load(ilcdpath + "\\sources\\"+uuid.ToString() + version + ".xml");
        }

        public IEnumerable<unitGroupDataSet> AllUnitGroup
        {
            get
            {
                string[] files = Directory.GetFiles(ilcdpath + "\\unitgroups", "*.xml");
                foreach (var file in files)
                {
                    yield return unitGroupDataSet.Load(file);
                }
            }
        }

        public unitGroupDataSet GetUnitGroup(Guid uuid)
        {
            return unitGroupDataSet.Load(ilcdpath + "\\unitgroups\\"+uuid.ToString() + version + ".xml");
        }

        private CategorySystem _ILCDClassification;
        public CategorySystem ILCDClassification
        {
            get
            {
                if (_ILCDClassification==null)
                    _ILCDClassification=CategorySystem.Load(ilcdpath + "\\ILCDClassification.xml");
                return _ILCDClassification;
            }
        }

        private CategorySystem _ILCDFlowCategorization;
        public CategorySystem ILCDFlowCategorization
        {
            get
            {
                if (_ILCDFlowCategorization==null)
                    _ILCDFlowCategorization=CategorySystem.Load(ilcdpath + "\\ILCDFlowCategorization.xml");
                return _ILCDFlowCategorization;
            }
        }

        private ILCDLocations _ILCDLocations;
        public ILCDLocations ILCDLocations
        {
            get
            {
                if (_ILCDLocations==null)
                    _ILCDLocations=ILCDLocations.Load(ilcdpath + "\\ILCDLocations.xml");
                return _ILCDLocations;
            }
        }
    }
}
